package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.logic.cross.CrossGeneralActivityUnionRankModel;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.CrossGeneralActivityRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manager.union.CrossUnionActivityDataMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;

@Cmd(code = Protocol.C_CROSS_GENERAL_UNION_MY_RANK, desc = "我的排名")
public class CrossGeneralActivityUnionMyRankCmd extends CrossCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        long userId = packet.getPlayerId();
        StrongholdWarProto.CrossStrongholdActivityUnionRankListReqMsg req = StrongholdWarProto.CrossStrongholdActivityUnionRankListReqMsg.parseFrom(packet.getBytes());
        int activityId = req.getActivityId();
        int conditionType = req.getConditionType();
        String unionUid = "";
        UnionActivityConditionData conditionData = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, conditionType, userId);
        if (conditionData != null) {
            unionUid = conditionData.getUnionUid();
        }
        int unionRank = CrossGeneralActivityRankMgr.getCrossActivityUnionRank(activityId, conditionType, serverId, unionUid);
        StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.Builder msg = StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.newBuilder();
        msg.setMyRank(unionRank);
        CrossGeneralActivityUnionRankModel model = CrossGeneralActivityRankMgr.getCrossActivityUnionRankListModel(activityId, conditionType);
        BigInteger myUnionValue = model.getMyUnionValue(serverId, unionUid);
        msg.setValue(myUnionValue.toString());
        //UnionActivityRankProto.UnionActivityRankListMsg.Builder rankListMsg = model.getRankListMsg();
        //msg.setRankList(rankListMsg);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(ClientProtocol.U_CROSS_GENERAL_ACTIVITY_UNION_RANK, msg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);
    }


    @Override
    public void sendData() {

    }
}
